package com.smarthospital.service.impl;

import com.alicp.jetcache.anno.CacheRefresh;
import com.github.pagehelper.PageInfo;
import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;
import com.smarthospital.common.util.IDGeneratorUtil;
import com.smarthospital.common.util.SHDateUtil;
import com.smarthospital.contants.SHConst;
import com.smarthospital.dao.daoService.*;
import com.smarthospital.entity.dto.DutiesDTO;
import com.smarthospital.entity.pojo.*;
import com.smarthospital.entity.vo.UserVO;
import com.smarthospital.service.OptService;
import com.smarthospital.service.SHDictionaryService;
import com.smarthospital.service.UserService;
import com.smarthospital.web.inteceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
 * 作    者： yingming shen
 * 修改时间： 2020/8/16 10:53.
 * 描   述：
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDaoService userDaoService;

    @Autowired
    private HospitalDaoService hospitalDaoService;

    @Autowired
    private SHDictionaryService dictionaryService;


//    @Cached(name = JetCacheNames.name1 ,key = "#id", cacheType =CacheType.BOTH,timeUnit = TimeUnit.SECONDS,expire = 120)
//    @CacheRefresh(refresh = 30, timeUnit = TimeUnit.SECONDS)
    @Override
    public User getUser(){
        return userDaoService.selectById(LoginInterceptor.getUser().getId());
    }

    @Override
    public User userInfo() {
        User user = LoginInterceptor.getUser();
        user = dictionaryService.resolveUserDictionFields(user);
        return user;
    }

    @Override
    public void updateUser(UserVO user) {
//        //校验短信
//        String code = user.getCode();
//        if (StringUtils.isEmpty(code)||StringUtils.isEmpty(user.getCheckPhone())) {
//            throw new CommonException(CodeMsg.ERROR_OPT_MISS);
//        }
        //不报错就是通过
//        optService.checkCode(LoginInterceptor.getUser().getId(),user.getCheckPhone(),SenceTypeEnum.SENCE_TYPE_0.getType(),code);
       //更新数据
        if (user.getId() == null) {
            throw new CommonException(CodeMsg.ERROR_PARAM);
        }
        userDaoService.update(user);
    }

    public User initUser(String openid) {
        User user = User.builder()
                //生成注册id
                .registerId(IDGeneratorUtil.getRegistId())
                .oppenId(openid)
                .createTime(SHDateUtil.now_yyyMMddHHmmss())
                .updateTime(SHDateUtil.now_yyyMMddHHmmss())
                .creator("self")
                .updator("self")
                .build();
        userDaoService.insertSelective(user);
        return user;
    }



    @Override
    public PageInfo<Hospital> querHospitals(Integer pageSize, Integer pageNum) {
        return hospitalDaoService.querHospitals(pageSize,pageNum);
    }

}
